<?php
/*
Plugin Name: XStandard
Plugin URI: http://xstandard.com/
Description: Integrates XStandard Editor into WordPress. XStandard is a standards-compliant and flexible WYSIWYG-Editor which produces markup that validates against XHTML 1.0 strict / XHTML 1.1 specifications. Administrators can configure WYSIWYGs on the <a href="options-general.php?page=xstandard.php">XStandard Options Page</a> - Users set the WYSIWYG-Editor on their <a href="profile.php">Profile Page</a>. 
Version: 0.3 beta
Author: Erik P&ouml;hler
Author URI: http://www.contactsheet.de
*/

/*__________________________________________________________________________________
 |                                                                                  |
 |      Copyright 2007  Erik Pöhler  (email : xstandard@contactsheet.de)            |
 |                                                                                  |
 |   This program is free software; you can redistribute it and/or modify           |
 |   it under the terms of the GNU General Public License as published by           |
 |   the Free Software Foundation; either version 2 of the License, or              |
 |   (at your option) any later version.                                            |
 |                                                                                  |
 |   This program is distributed in the hope that it will be useful,                |
 |   but WITHOUT ANY WARRANTY; without even the implied warranty of                 |
 |   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                  |
 |   GNU General Public License for more details.                                   |
 |                                                                                  |
 |   You should have received a copy of the GNU General Public License              |
 |   along with this program; if not, write to the Free Software                    |
 |   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA      |
 |__________________________________________________________________________________|
*/


/* TODO
   offer an API or possibility to include other WYSIWYGs
   (eg. use folder-structure)
*/

/*  debugging: don't do this at home. 
    error_reporting(E_ALL); 
    ini_set('display_errors', 1);
    echo XS_LIBRARY_FOLDER . '<br />';
    echo XS_BASE_URL .'<br />';
*/ 
if ( substr(get_bloginfo('wpurl'),-1,1) !== '/' )
{
  $WPURL_WITH_SLASH = get_bloginfo('wpurl') . '/';
}
else 
{
  $WPURL_WITH_SLASH = get_bloginfo('wpurl');
}
$WPUPLOADPATH = get_option('upload_path');
define('XSTANDARD_BASE', dirname(plugin_basename(__FILE__)));
define('XSTANDARD_PATH', ABSPATH . 'wp-content/plugins/' . XSTANDARD_BASE);
define('XSTANDARD_URL', get_bloginfo('wpurl') . '/wp-content/plugins/' . XSTANDARD_BASE);
define('XSTANDARD_UPLOADPATH', $WPUPLOADPATH);
define('XSTANDARD_WPURL', $WPURL_WITH_SLASH);
include("config.php");

load_plugin_textdomain('xstandard',$path='wp-content/plugins/' . XSTANDARD_BASE . '/l10n');

$xstandard_version = "1.7.1.0";
$xstandard_lang = explode('_',get_locale(),-1);
$xstandard_lang = $xstandard_lang[0];
$plugin_name = __('XStandard for WordPress','xstandard');
$plugin_version = "0.3 beta";
$plugin_author = "Erik Pöhler";
$plugin_author_uri = "http://contactsheet.de/";
$plugin_uri = "http://contactsheet.de/plugins/xstandard-for-wordpress.xhtml";

update_usermeta($current_user->ID,'rich_editing','false');
$xstandard_default_options['OneSetting'] = "1";
$xstandard_default_options['UseRichEditor'] = "xstandard";
$xstandard_default_options['WysiwygToolbarDefaultOrder'] = "open-document,save,new-document,print,separator,cut,copy,paste,separator,strong,em,sub,sup,underline,strikethrough,euro,pound,yen,copyright,registered-trade-mark,trade-mark,separator,blockquote,undo-blockquote,indent,separator,align-left,align-center,align-right,textbox,ordered-list,unordered-list,separator,layout-table,draw-layout-table,data-table,draw-data-table,separator,image,photo,line,hyperlink,attachment,directory,undo,wysiwyg,source,preview,screen-reader,spellchecker,properties,validate,wizard,expand,help";
$xstandard_user_default_options = $xstandard_default_options['UseRichEditor'];

/*_____________________________________
 |                                     |
 |               START                 |
 |_____________________________________|
*/

$xstandard_options = get_option('xstandard_options');
if ($xstandard_options) {
foreach($xstandard_options as $k => $v) {
	   $xstandard_options[$k] = $v;
    }
}
if ( strlen($xstandard_options['WysiwygToolbarDefaultOrder']) <= 0 )
{
  $xstandard_options = $xstandard_default_options;
  update_option('xstandard_options',$xstandard_options,'XStandard Configuration','yes');
}

if ( ( empty($xstandard_options['UseRichEditor']) ) || ( strlen($xstandard_options['UseRichEditor']) <= 0 ) )
{
  $xstandard_options = $xstandard_default_options;
  update_option('xstandard_options',$xstandard_options,'XStandard Configuration','yes');
  update_usermeta($current_user->ID,'xstandard_use_rich_editor',$xstandard_user_default_options);
  update_usermeta($current_user->ID,'rich_editing','false');
}

global $current_user;

if (get_usermeta($current_user->ID,'xstandard_use_rich_editor')) {
$current_user_options = get_usermeta($current_user->ID,'xstandard_use_rich_editor');  
}
if ( empty($current_user_options) || ( strlen($current_user_options) <= 0 ) )
{ 
  $xstandard_use_rich_editor = $xstandard_default_options['UseRichEditor'];
  update_usermeta($current_user->ID,'xstandard_use_rich_editor',$xstandard_use_rich_editor);  
}
else
{
  $xstandard_user_options = get_usermeta($current_user->ID,'xstandard_use_rich_editor');
}
  
$xstandard_stored_options = get_option('xstandard_options');
$xstandard_stored_user_options = get_usermeta($current_user->ID,'xstandard_use_rich_editor');

/* if ( is_array($xstandard_stored_options) && ( is_string($xstandard_stored_user_options) && (strlen($xstandard_stored_user_options) >= 0) ) )
{
  $message[] = __('Status: Both options from the db seem to be of correct type: xstandard_options is an array and the usermeta is a string.','xstandard');
}
else
{
  $message[] = __('Status: Something went wrong with the stored options. wrong type of data.','xstandard');
} */

function xstandardGetName($wysiwyg = false)
{
  
  if ($wysiwyg === false)
  {
    return;        
  }
  else
  {
    switch ($wysiwyg)
    {
      case 'xstandard' :      
        $wysiwyg_name = __('XStandard for WordPress','xstandard'); 
        continue;
      case 'tiny_mce' :      
        $wysiwyg_name = 'TinyMCE';      
        continue;
      case 'quicktags' :      
        $wysiwyg_name = 'quicktags.js'; 
        continue;
      case 'none' :
        $wysiwyg_name = __('none','xstandard');      
    }
    return $wysiwyg_name;
  }
}

/*_____________________________________
 |                                     |
 |            ADMIN PAGE               |
 |_____________________________________|
*/

function xstandardOptionsPage()
{
/* set plugin-info */
  global $plugin_author, $plugin_author_uri, $plugin_uri, $plugin_version, $plugin_name, $xstandard_lang; // $xstandard_lang, $current_user, $xstandard_options, $xstandard_user_options;  
  $message='';
  $xstandard_options = get_option('xstandard_options');
  if($xstandard_options)
  {
    foreach($xstandard_options as $k => $v) {
	   $xstandard_options[$k]=$v;
    }
  }

/* process form data */

  if ( isset( $_POST['OneSetting'] ) )
  {
  $xstandard_options['OneSetting'] = $_POST['OneSetting'];
  update_option('xstandard_options', $xstandard_options, 'XStandard Configuration', 'no');
  if ($xstandard_options['OneSetting'] == '1' && $xstandard_options['UseRichEditor'] !== 'tiny_mce') {
        update_usermeta($current_user->ID,'rich_editing','false');
    }
  $updated = true;
  }
  
  if ( ( ( $xstandard_options['UseRichEditor'] !== 'tiny_mce' ) && ( $xstandard_options['OneSetting'] == '1' ) ) || ( ( get_user_option('xstandard_use_rich_editor') !== 'tiny_mce' ) && ( $xstandard_options['OneSetting'] == '0' ) ) )
  {  
    update_usermeta($current_user->ID,'rich_editing','false');    
  }
  else
  {
    update_usermeta($current_user->ID,'rich_editing','true');
  }
  
  if ( $updated )
  {
    $xstandard_options = get_option('xstandard_options');
    if ( $xstandard_options )
    {
      foreach($xstandard_options as $k => $v)
      {
        $xstandard_options[$k] = $v;
      }
    }
  }
  
  if ( ( $updated ) && ( $xstandard_options['OneSetting'] == '1' ) )
  {
    $message[] = __('Your WYSIWYG settings are now valid for all users.','xstandard');
  }
  else if ( ( $updated ) && ( $xstandard_options['OneSetting'] == '0' ) )
  {
    $message[] = __('Users can choose their favourite WYSIWYG editor.','xstandard');
  }
  
  if (is_file(XSTANDARD_PATH . '/l10n/logo-xstandard-' . $xstandard_lang . '.gif'))
  {
    $logo_path = XSTANDARD_URL . '/l10n/logo-xstandard-' . $xstandard_lang . '.gif';  
  }
  else
  {
    $logo_path = XSTANDARD_URL . '/l10n/logo-xstandard-en.gif';
  }
  
  if ( is_array($message) )
  {
    foreach ($message as $eachmessage)
    {  
      if ( !empty($eachmessage) )
      {
        print '<div class="updated fade"><p><strong>'.$eachmessage.'</strong></p></div>';
      }
	  }
	}	
  ?>
  <div class="wrap">  
  
  <h2 style="margin-top:-7px;margin-bottom:30px;">
    <img src="<?php echo $logo_path; ?>" alt="XStandard" />
  </h2>
  <p><?php echo __('Welcome to the administration panel for &quot;XStandard for WordPress&quot;','xstandard').' v'.$plugin_version; ?></p>  
  <form method="post" name="xstandard_options" action="<?php $_SERVER["REQUEST_URI"]; ?>">  
  <table class="widefat">
    <thead>
      <tr>
        <th scope="col"><?php _e('Setting','xstandard'); ?></th>
        <th scope="col" style="text-align:center"><?php _e('Value','xstandard'); ?></th>
        <th scope="col"><?php _e('Description','xstandard'); ?></th>
      </tr>
    </thead>
    
    <tbody>
      <tr class="alternate">
        <th scope="row"><?php _e('One Setting for all Users?','xstandard'); ?></th>
        <td align="center">
          <label>
            <input type="radio" id="OneSetting-yes" name="OneSetting" value="1" <?php checked('1',$xstandard_options['OneSetting']); ?> />
            <?php _e('Yes','xstandard'); ?>
          </label>
          <label for="OneSetting-no">
            <input type="radio" id="OneSetting-no" name="OneSetting" value="0" <?php checked('0',$xstandard_options['OneSetting']); ?> />
            <?php _e('No','xstandard') ?>
          </label>
        </td>
        <td><?php _e('If set to \'yes\' your settings will be valid vor every contributor, author, editor, ...<br />If set to \'no\' every user can define his/her preferred Editor on their profile-page.','xstandard'); ?></td>
      </tr>
      
    <tr><th colspan="3" class="wysiwygtoolbar">All available Buttons:</th></tr>

    
    <tr>
      <td colspan="3">
        <?php
        $wysiwygToolbarOrder = explode(',',$xstandard_options['WysiwygToolbarDefaultOrder']);        
        foreach ( $wysiwygToolbarOrder as $toolbarButton ) {
          if ($toolbarButton !== 'separator')
          {
          echo '<img style="margin:1px;width:16px;height:16px;" src="' . XSTANDARD_URL . '/xstandard/img/' . $toolbarButton . '.gif" border="1" width="16" height="16" alt="' . $toolbarButton . '" title="' . $toolbarButton . '" id="' . $toolbarButton . '" />';
          }
          else
          {
          echo '<img style="margin:1px;width:16px;height:16px;" src="' . XSTANDARD_URL . '/xstandard/img/' . $toolbarButton . '.gif" border="1" width="16" height="16" alt="' . $toolbarButton . '" title="' . $toolbarButton . '" />';
          }
        }
        ?>
      </td>
    </tr>
    </tbody>
  </table>  
<?php  
  
print '<p>'
     .'$xstandard_options["OneSetting"] =  ' . $xstandard_options['OneSetting'] . '<br />'
     .'$xstandard_options["UseRichEditor"] = ' . $xstandard_options["UseRichEditor"] . '<br />'
     .'get_user_option("rich_editing") = ' . get_user_option('rich_editing') . '<br />'  
     .'xstandard_use_rich_editor (user option/usermeta) = '.get_user_option('xstandard_use_rich_editor').'<br /></p>';
  
print '<div class="submit"><input type="submit" name="xstandard_options" value="'.__('Submit Options','xstandard').'" /></div>'
     .'<p><small>'.sprintf(__('<strong>%s</strong> v%s &#124; Plugin-Author: <a href="%s" title="%s">%s</a> &#124; Plugin-Support: <a href="%s" title="%s">%s</a> &#124; <a href="http://xstandard.com/">XStandard</a> is a product of Belus Technology','xstandard'), $plugin_name, $plugin_version, $plugin_author_uri, $plugin_author, $plugin_author, $plugin_uri, $plugin_name, $plugin_name).'</small></p>'
     .'</form>'
     .'</div>';
}

/*_____________________________________
 |                                     |
 |         XSTANDARD INTERFACE         |
 |_____________________________________|
*/

function xstandardAddStuffToAdminHead()
{
  global $xstandard_version;
  
  $xstandard_options = get_option('xstandard_options');
  if($xstandard_options) {
	 foreach($xstandard_options as $k => $v) {
		  $xstandard_options[$k]=$v;	
	 }
  }  

  $xlatest = $xstandard_version;      
  /*
    only load the snippets if xstandard is the editor of choice
  */
    if ( ( ( $xstandard_options['OneSetting'] == '0' ) && ( get_user_option('xstandard_use_rich_editor') == 'xstandard' ) ) || ( ( $xstandard_options['OneSetting'] == '1' ) && ( $xstandard_options['UseRichEditor'] == 'xstandard' ) ) )
    {
  /*
      make sure we're on a post/page-editing page...
  */
      if ( ( !( strpos($_SERVER['REQUEST_URI'], 'post.php') === false ) ) || ( !( strpos($_SERVER['REQUEST_URI'], 'post-new.php') === false ) ) || ( !( strpos($_SERVER['REQUEST_URI'], 'page.php') === false ) ) || ( !( strpos($_SERVER['REQUEST_URI'], 'page-new.php') === false ) ) )
      {
      ?>      
<style type="text/css">
#postdivrich, #postdiv {
  display:none;  
}
fieldset#xstandard {
  position:relative;
}
</style>  

<!--[if !IE]>-->
<script type="text/javascript" src="<?php echo XSTANDARD_URL; ?>/xstandard.js"></script>
<!--<![endif]-->

<script type="text/javascript">  
//<![CDATA[
function updateXStandard() {
  //Set the latest version
  latestVersion = '<?php echo $xlatest; ?>';

  //Check if browser is Firefox
  if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1) {
	  //Go through all the object tags on the page
		var objects = document.getElementsByTagName('object');
		for (var i=0; i<objects.length; i++) {
		var obj = objects[i];
		try {
		  //Check if object is XStandard
			if(typeof(obj.EscapeUnicode) == 'undefined') {
        throw "Error"
			} else {
			//Check the editor is the latest version
			if (obj.Version != latestVersion) {
        //Redirect to update page
				window.location.replace('http://xstandard.com/upgrade-firefox-version/');
				break;
			   }
			  }			
		  }
			catch(er) {}
    }
  }
}
//]]>
</script>
  
  <?php
    }
  }

/*  only loads the css-snippet on a user-profile
    this should at least hide the #rich_editing input in Gecko-based Browsers...
    TODO: fix others that show the label - maybe JS can help here... 
*/ 
  if ( !( strpos($_SERVER['REQUEST_URI'], 'profile.php') === false ) )
  {
  
  ?>
  <style type="text/css">
  #rich_editing {
    display:none;
  }
  label[for=rich_editing] {
    display:none;
  }
  </style>
  <?php
  } 
}

function xstandardLoadInterface()
{  
  global $post, $xstandard_lang, $xstandard_version;
  
  $xstandard_options = get_option('xstandard_options');
  if($xstandard_options) {
	 foreach($xstandard_options AS $k=>$v) {
		  $xstandard_options[$k]=$v;	
	 }
  }
  if ( ($xstandard_options['UseRichEditor'] == 'xstandard' && $xstandard_options['OneSetting'] == '1') || (get_user_option('xstandard_use_rich_editor') == 'xstandard' && $xstandard_options['OneSetting'] == '0') )
  {    
    $xv = '#Version='.str_replace('.',',',$xstandard_version);
    
    /* check if options present else set a default */
    
    // Height
    if ($xstandard_options['Height'])
    {
      $xh = $xstandard_options['Height'];   
    }
    else
    {
      $xh = "500";
    }
    // Styles
    if ($xstandard_options['Styles'])
    {
      $xs = $xstandard_options['Styles'];
    }
    else
    {
      $xs = "styles.xml";
    }
    // CSS
    if ($xstandard_options['CSS'])
    {
      $xf = $xstandard_options['CSS'];
    }
    else
    {
      $xf = "format.css";
    }
    // ToolbarWysiwyg
    if ($xstandard_options['ToolbarWysiwyg'])
    {
      $xt_wysiwyyg = $xstandard_options['ToolbarWysiwyg'];
    }
    else 
    {
      $xt_wysiwyg = "ordered-list,unordered-list,,layout-table,data-table,image,line,hyperlink,attachment,directory,undo,,wysiwyg,source,preview,screen-reader,help";
    }
    // Class Left
    if ($xstandard_options['ClassLeft'])
    {
      $xleft = $xstandard_options['ClassLeft'];
    }
    else 
    {
      $xleft = "left";
    }
    // Class Right
    if ($xstandard_options['ClassRight'])
    {
      $xright = $xstandard_options['ClassRight'];
    }
    else 
    {
      $xright = "right";
    }
    // Enable Timestamp
    if ($xstandard_options['EnableTimestamp'])
    {
      $xtstamp = $xstandard_options['EnableTimestamp'];
    }
    else
    {
      $xtstamp = "no";
    }
    
    // Text Indent
    /* trying to get the posts content */
    if (isset($_POST['post']))
    {
    $postid = $_POST['post'];
    $post = &get_post($postid);    
    }
                
    if ( !(strpos($_SERVER["HTTP_USER_AGENT"], "Win95") === false) || !(strpos($_SERVER["HTTP_USER_AGENT"], "Windows 95") === false) )
    {
      $xstandard_codebase = 'XStandardAnsi.cab'.$xv;
    }
    elseif ( !(strpos($_SERVER["HTTP_USER_AGENT"], "Win98") === false) || !(strpos($_SERVER["HTTP_USER_AGENT"], "Windows 98") === false ) )
    {
      $xstandard_codebase = 'XStandardAnsi.cab'.$xv;
    }
    elseif ( !(strpos($_SERVER["HTTP_USER_AGENT"], "WinME") === false) || !(strpos($_SERVER["HTTP_USER_AGENT"], "Windows ME") === false ) )
    {
      $xstandard_codebase = 'XStandardAnsi.cab'.$xv;
    }
    elseif ( !(strpos($_SERVER["HTTP_USER_AGENT"], "WinNT 5.0") === false) || !(strpos($_SERVER["HTTP_USER_AGENT"], "Windows NT 5.0") === false) )
    {
      $xstandard_codebase = 'XStandard.cab'.$xv;
    }
    elseif ( !(strpos($_SERVER["HTTP_USER_AGENT"], "WinNT 5.1") === false) || !(strpos($_SERVER["HTTP_USER_AGENT"], "Windows NT 5.1") === false) )
    {
      $xstandard_codebase = 'XStandard.cab'.$xv;
    }
    elseif ( !(strpos($_SERVER["HTTP_USER_AGENT"], "WinNT 5.2") === false) || !(strpos($_SERVER["HTTP_USER_AGENT"], "Windows NT 5.2") === false) )
    {
      $xstandard_codebase = 'XStandard.cab'.$xv;
    }
    elseif ( !(strpos($_SERVER["HTTP_USER_AGENT"], "Macintosh") === false) || !(strpos($_SERVER["HTTP_USER_AGENT"], "Mac_PowerPC") === false) )
    {
      $xstandard_codebase = '';
    }
    elseif ( !(strpos($_SERVER["HTTP_USER_AGENT"], "Unix") === false ) )
    {
      $xstandard_codebase = '';
    }
    else
    {
    $xstandard_codebase = '';
    }
    
  ?>    
<p style="padding:.1em .3em;margin:0;"><?php _e('Post', 'xstandard'); ?></p>
<div>
  <?php
    if ( $xstandard_codebase !== '' )
    {
    ?>
<object type="application/x-xstandard" width="100%" height="<?php echo $xh; ?>" name="content" codebase="<?php echo XSTANDARD_URL . '/xstandard/Bin/' . $xstandard_codebase; ?>">
    <?php
    }
    else
    { 
    ?>
<object type="application/x-xstandard" width="100%" height="<?php echo $xh; ?>" name="content" style="border:1px solid #686868;">
    <?php
    }
    ?>
  <param name="Styles" value="<?php echo XSTANDARD_URL . '/xstandard/styles/' . $xs; ?>" />
  <param name="CSS" value="<?php echo XSTANDARD_URL . '/xstandard/css/'. $xf; ?>" />
  <!-- Who the fuck is Joomla? :D -->
  <param name="CMSCode" value="065126D6-357D-46FC-AF74-A1F5B2D5036E" />
  <!-- These are implemented but haven't been tested enough -->
  <param name="CMSImageLibraryURL" value="<?php echo XSTANDARD_URL; ?>/xstandard/libraries/cmsimagelibrary.php" />
  <param name="CMSAttachmentLibraryURL" value="<?php echo XSTANDARD_URL; ?>/xstandard/libraries/cmsattachmentlibrary.php" />
  <param name="CMSDirectoryURL" value="<?php echo XSTANDARD_URL; ?>/xstandard/libraries/cmsdirectory.php" />
  <!-- end of not enough tested stuff -->
  <param name="Lang" value="<?php echo $xstandard_lang; ?>" />
  <param name="Toolbar" value="<?php echo $xt_wysiwyg; ?>" />
  <param name="EscapeUnicode" value="true" />  
  <param name="ClassImageFloatLeft" value="<?php echo $xleft; ?>" />
  <param name="ClassImageFloatRight" value="<?php echo $xright; ?>" />
	<param name="IndentOutput" value="yes"/>
	<param name="ShowStyles" value="yes" />
  <param name="BackgroundColor" value="#FFF" />
	<param name="BorderColor" value="#b4b4b4" />
	<param name="PreviewXSLT" value="<?php echo XSTANDARD_URL; ?>/xstandard/preview.xsl" />
	<param name="EnablePasteMarkup" value="true" />
  <param name="EnableTimestamp" value="<?php echo $xtstamp; ?>" />
	<!--
  <param name="Localization" value="<?php // echo $localization; ?>" />
  either this way (do localization with poedit) or the default (use integrated l10n data)
  -->
	<param name="Value" value="<?php echo $post->post_content; ?>" /> 
  <textarea name="content" rows="5" cols="50"><?php echo $post->post_content; ?></textarea>  
</object>
</div>
  <?php
  } 
}

/*_____________________________________
 |                                     |
 |           USER PROFILE              |
 |_____________________________________|
*/
 
function xstandardAddProfileSelect() {
  global $xstandard_lang;
  /** get saved options **/ 
  $xstandard_options = get_option('xstandard_options');
  if ( $xstandard_options ) {
	   foreach ( $xstandard_options AS $k=>$v ) {
		    $xstandard_options[$k]=$v;	
	   }
  }   
  $xstandard_lang = explode('_',$xstandard_lang,2);
  if (strlen($xstandard_lang[0]) <= 0)
  {
    $xstandard_lang = 'en';
  }
  else
  {
    $xstandard_lang = $xstandard_lang[0];
  }  
    
  /* for user-wysiwyg */
  if ( $xstandard_options['OneSetting'] = "0" )
  {    
    $curwysiwyg = get_user_option('xstandard_use_rich_editor');   
    $wysiwyg_desc = xstandardGetName($curwysiwyg);
    if ($curwysiwyg !== 'xstandard')
    {
      $xstandard_lang = 'en';
    }
    $wysiwyg_logo = XSTANDARD_URL . '/l10n/logo-' . $curwysiwyg . '-' . $xstandard_lang . '.gif';    
  }
  /* for global-wysiwyg */    
  if ( $xstandard_options['OneSetting'] = "1" ) 
  {
    $curwysiwyg = $xstandard_options['UseRichEditor'];   
    $wysiwyg_desc = xstandardGetName($curwysiwyg);
    if ($curwysiwyg !== 'xstandard')
    {
      $xstandard_lang = 'en';
    }
    $wysiwyg_logo = XSTANDARD_URL . '/l10n/logo-' . $curwysiwyg . '-' . $xstandard_lang . '.gif'; 
  }    
  ?>  
  
  <?php if ( $xstandard_options['OneSetting'] == '0' ) {
    $xstandard_user_options = get_user_option('xstandard_use_rich_editor');     
  ?>
<fieldset>
<legend><?php _e('Your favorite WYSIWYG-Editor','xstandard'); ?></legend>
<p>
  <img src="<?php echo $wysiwyg_logo; ?>" alt="WYSIWYG" title="WYSIWYG" style="margin-bottom:15px;" />
  <?php echo __('Your WYSIWYG-Editor is set to','xstandard') . ' "' . $wysiwyg_desc . '<br />"'; ?>
</p>  
<p>
  <select name="xstandard_use_rich_editor" style="width:100%;margin:.5 0;padding:.3em">
  <option <?php selected('quicktags', $xstandard_user_options); ?> value="quicktags"><?php echo xstandardGetName('quicktags'); ?></option>
  <option <?php selected('tiny_mce', $xstandard_user_options); ?> value="tiny_mce"><?php echo xstandardGetName('tiny_mce'); ?></option>
  <option <?php selected('xstandard', $xstandard_user_options); ?> value="xstandard"><?php echo xstandardGetName('xstandard'); ?></option>
  <option <?php selected('none', $xstandard_user_options); ?> value="none"><?php echo xstandardGetName('none'); ?></option>
  </select>
</p>
</fieldset>
<?php
  }
  else if ($xstandard_options['OneSetting'] == '1')
  { ?>  
  <fieldset>
  <legend><?php _e('Your WYSIWYG-Editor','xstandard'); ?></legend>
  <p>
    <img src="<?php echo $wysiwyg_logo; ?>" alt="WYSIWYG" title="WYSIWYG" style="margin-bottom:15px;" />
    <?php echo __('Your WYSIWYG-Editor is set to','xstandard') . ' "' . $wysiwyg_desc . '"<br />'; ?>
  </p>   
  <em><?php _e('switching WYSIWYG-Editors is either currently disabled or you don\'t have the capability to do so.','xstandard'); ?></em>  
  </fieldset>
  <?php
  }
}

function xstandardPersonalOptionsClearfloat() {
  echo '<div style="clear:both;"></div>';
} 

function xstandardUpdateProfile() {
  global $current_user, $xstandard_options, $xstandard_default_options;
  $xstandard_options = get_option('xstandard_options');
  if($xstandard_options) {
	 foreach($xstandard_options AS $k=>$v) {
		  $xstandard_options[$k]=$v;	
	 }
  }
  
  if (isset($_POST['xstandard_use_rich_editor'])) {
    $xstandard_user_options = $_POST['xstandard_use_rich_editor'];   
    if ($xstandard_options['OneSetting'] == '0') {
        update_usermeta($current_user->ID,'xstandard_use_rich_editor',$xstandard_user_options);
        if ($xstandard_user_options == 'tiny_mce') {
            update_usermeta($current_user->ID,'rich_editing','true');
        }
        else
        {
            update_usermeta($current_user->ID,'rich_editing','false');
        }
    }
  }  
}

/*_____________________________________
 |                                     |
 |    REMOVE PREVIEW AND UPLOADER?     |
 |_____________________________________|
*/

function xstandard_remove_uploading($content)
{
	$content = preg_replace('/<iframe id="uploading"(.*)<\/iframe>/', '', $content);
	return $content;
}

function xstandard_kill_preview($content)
{
	global $post;
	$content = preg_replace("/<div[^>]*?id='preview'.*?<\/div>/mis", '', $content);
	if ( 3664 <= $wp_db_version )
  {
		$content = preg_replace('/<a href="#preview-post">/mis', '', $content);
	}
  else
  {
		$content = preg_replace('/<div id="moremeta">/', '<div id="moremeta">', $content);
	}
	return $content;
}

if (strstr($_SERVER['REQUEST_URI'], 'wp-admin/post.php') || (strstr($_SERVER['REQUEST_URI'], 'wp-admin/page.php')) || (strstr($_SERVER['REQUEST_URI'], 'wp-admin/page-new.php')) || (strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php')))
{	
  ob_start('xstandard_remove_uploading');	
}

if((strstr($_SERVER['REQUEST_URI'], 'wp-admin/post.php')) || (strstr($_SERVER['REQUEST_URI'], 'wp-admin/page.php')))
{
	ob_start('xstandard_kill_preview');
}

function xstandardAddOptionsPage()
{
  if (function_exists('add_options_page') && current_user_can('level_8')) 
  {
		add_options_page('XStandard','XStandard', 8, basename(__FILE__), 'xstandardOptionsPage');	
	}
}

/*_____________________________________
 |                                     |
 |        FILTERS AND ACTIONS          |
 |_____________________________________|
*/

if ( (get_usermeta($current_user->ID,'xstandard_use_rich_editor') == 'xstandard' && $xstandard_options['OneSetting']== '0') || ($xstandard_options['UseRichEditor'] == 'xstandard' && $xstandard_options['OneSetting'] == '1' ) )
{
/* TODO:
   Check if this works or is enough. (remeber there are dozen of content-filters)
   What about filters of additional plugins? such as Intypo...?
*/  
  // remove_filter('the_excerpt', 'wpautop');  
  // remove_filter('the_excerpt', 'wptexturize');
  // remove_filter('the_content', 'wpautop');
  // remove_filter('the_content', 'wptexturize');  
}

add_action('admin_menu', 'xstandardAddOptionsPage');
add_action('admin_head','xstandardAddStuffToAdminHead');

add_action('profile_personal_options', 'xstandardAddProfileSelect', 1); 
add_action('profile_personal_options', 'xstandardPersonalOptionsClearfloat');
add_action('personal_options_update','xstandardUpdateProfile');

add_action('edit_form_advanced','xstandardLoadInterface',1);
add_action('edit_page_form','xstandardLoadInterface',1);
add_action('simple_edit_form','xstandardLoadInterface',1);
?>
